﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpf_buildtool
{
    /// <summary>
    /// file_nesasm3.xaml 的交互逻辑
    /// </summary>
    public partial class file_nesasm3 : UserControl
    {
        public file_nesasm3()
        {
            InitializeComponent();
        }
        public string FileName
        {
            get;
            set;
        }
        public string Fullname
        {
            get;
            private set;
        }
        class Doc : IDE.IDocument
        {
            public UIElement Editor
            {
                get;
                set;
            }

            public string Filename
            {
                get;
                set;
            }

            public bool writeable;
            public bool Save()
            {
                if (!writeable) return false;

                return (Editor as file_nesasm3).Save();
            }
            public event Action Change;
            public void DoChange()
            {
                Change?.Invoke();
            }
        }
        Doc thisdoc;
        public IDE.IDocument OpenFile(string filename, bool writeable)
        {
            this.editor.ShowLineNumbers = true;
            this.editor.Text = System.IO.File.ReadAllText(filename);
            using (var fs = System.IO.File.OpenRead("asm.xshd"))
            {
                using (var xmlreader = System.Xml.XmlReader.Create(fs))
                {
                    var xshd = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.LoadXshd(xmlreader);
                    var highlightdef=ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);

                    this.editor.SyntaxHighlighting = highlightdef;

                }

            }
            if (!writeable)
            {
                this.editor.IsReadOnly = true;
            }
            else
            {
                this.editor.TextChanged += (s, e) =>
                  {
                      thisdoc.DoChange();
                  };
            }
            this.Fullname = filename;
            this.FileName = System.IO.Path.GetFileName(filename);
            thisdoc = new Doc();
            thisdoc.Editor = this;
            thisdoc.Filename = filename;
            thisdoc.writeable = writeable;
            return thisdoc;
        }
        public bool Save()
        {
            try
            {
                System.IO.File.Delete(Fullname);
                System.IO.File.WriteAllText(Fullname, this.editor.Text);
                return true;
            }
            catch(Exception err)
            {
                return false;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var fs = System.IO.File.OpenRead("d:\\asm.xshd"))
                {
                    using (var xmlreader = System.Xml.XmlReader.Create(fs))
                    {
                        var xshd = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.LoadXshd(xmlreader);
                        var highlightdef = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);

                        this.editor.SyntaxHighlighting = highlightdef;

                    }

                }
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }
    }
}
